package com.zg.job.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/2/20 11:02
 */

@Slf4j
@Component
public class CustomJobExecutionListener extends JobExecutionListenerSupport {

    @Override
    public void beforeJob(org.springframework.batch.core.JobExecution  jobExecution) {
        // 作业开始前执行的逻辑
        log.info("Job [" + jobExecution.getJobInstance().getJobName() + "] is starting at: "
                + new Date());
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        // 作业结束后执行的逻辑
        if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
            log.info("Job [" + jobExecution.getJobInstance().getJobName() + "] completed successfully!");
        } else if (jobExecution.getStatus() == BatchStatus.FAILED) {
            log.error("Job [" + jobExecution.getJobInstance().getJobName() + "] failed! Exit status: "
                    + jobExecution.getExitStatus());
        }
        // 清理资源或发送通知
    }
}